home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / pilot-prc.c < prev    next >
C/C++ Source or Header  |  1997-05-23  |  7KB  |  296 lines

  1. /*
  2.  * Pilot .prc pack/unpack utility
  3.  * Based on pilot-file
  4.  * Additions by Kenneth Albanowski
  5.  *
  6.  * This is free software, licensed under the GNU Public License V2.
  7.  * See the file COPYING for details.
  8.  */
  9.           
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <time.h>
  14.  
  15. #include "pi-source.h"
  16. #include "pi-socket.h"
  17. #include "pi-dlp.h"
  18. #include "pi-file.h"
  19.  
  20. #ifdef sun
  21.   extern char* optarg;
  22.   extern int optind;
  23. #endif
  24.  
  25. void dump_header (struct pi_file *pf, struct DBInfo *ip);
  26. void dump_app_info (struct pi_file *pf, struct DBInfo *ip);
  27. void dump_sort_info (struct pi_file *pf, struct DBInfo *ip);
  28. void list_records (struct pi_file *pf, struct DBInfo *ip);
  29. void dump_record (struct pi_file *pf, struct DBInfo *ip, int record);
  30.  
  31. char *iso_time_str (time_t t);
  32. void dump (void *buf, int size);
  33.  
  34. char * progname;
  35.  
  36. void
  37. usage (void)
  38. {
  39.   fprintf (stderr, "usage: %s [-s] -p|-u dir file\n", progname);
  40.   fprintf (stderr, "  -p      pack the contents of a directory into the named file\n");
  41.   fprintf (stderr, "  -u      unpack the contents of the named file into a directory\n");
  42.   fprintf (stderr, "  -s      don't obey/generate sort list\n");
  43.   fprintf (stderr, "\nContents of directory may include files like these:\n");
  44.   fprintf (stderr, "  tSTRx12CD[.bin] (a resource with hex index)\n");
  45.   fprintf (stderr, "  tSTR10000[.bin] (a reousrce with decimal index)\n");
  46.   fprintf (stderr, "  sort            (a file containing the names of other files)\n");
  47.   exit (1);
  48. }
  49.  
  50. int hflag, aflag, sflag, vflag, lflag, rflag, rnum;
  51.  
  52. int
  53. main (int argc, char **argv)
  54. {
  55.   struct pi_file *pf;
  56.   int c;
  57.   char *name;
  58.   struct DBInfo info;
  59.   
  60.   progname = argv[0];
  61.  
  62.   while ((c = getopt (argc, argv, "haslr:v")) != EOF) {
  63.     switch (c) {
  64.     case 'h':
  65.       hflag = 1;
  66.       break;
  67.     case 'a':
  68.       aflag = 1;
  69.       break;
  70.     case 's':
  71.       sflag = 1;
  72.       break;
  73.     case 'v':
  74.       vflag = 1;
  75.       break;
  76.     case 'l':
  77.       lflag = 1;
  78.       break;
  79.     case 'r':
  80.       rflag = 1;
  81.       rnum = atoi(optarg);
  82.       break;
  83.     default:
  84.       usage ();
  85.     }
  86.   }
  87.  
  88.   if (optind >= argc)
  89.     usage ();
  90.   
  91.   name = argv[optind++];
  92.  
  93.   if (optind != argc)
  94.     usage ();
  95.  
  96.   if ((pf = pi_file_open (name)) == NULL) {
  97.     fprintf (stderr, "can't open %s\n", name);
  98.     exit (1);
  99.   }
  100.  
  101.   if (pi_file_get_info (pf, &info) < 0) {
  102.     fprintf (stderr, "can't get info\n\n");
  103.     exit (1);
  104.   }
  105.   
  106.   if (hflag || vflag)
  107.     dump_header (pf, &info);
  108.  
  109.   if (aflag || vflag)
  110.     dump_app_info (pf, &info);
  111.  
  112.   if (sflag || vflag)
  113.     dump_sort_info (pf, &info);
  114.  
  115.   if (lflag || vflag)
  116.     list_records(pf, &info);
  117.   
  118.   if (rflag)
  119.     dump_record(pf, &info, rnum);
  120.     
  121.   return (0);
  122. }
  123.  
  124. char *
  125. iso_time_str (time_t t)
  126. {
  127.   struct tm tm;
  128.   static char buf[50];
  129.  
  130.   tm = *localtime (&t);
  131.   sprintf (buf, "%04d-%02d-%02d %02d:%02d:%02d",
  132.        tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  133.        tm.tm_hour, tm.tm_min, tm.tm_sec);
  134.   return (buf);
  135. }
  136.  
  137. void
  138. dump (void *buf, int n)
  139. {
  140.   int i, j, c;
  141.  
  142.   for (i = 0; i < n; i += 16) {
  143.     printf ("%04x: ", i);
  144.     for (j = 0; j < 16; j++) {
  145.       if (i+j < n)
  146.     printf ("%02x ", ((unsigned char *)buf)[i+j]);
  147.       else
  148.     printf ("   ");
  149.     }
  150.     printf ("  ");
  151.     for (j = 0; j < 16 && i+j < n; j++) {
  152.       c = ((unsigned char *)buf)[i+j] & 0x7f;
  153.       if (c < ' ' || c >= 0x7f)
  154.     putchar ('.');
  155.       else
  156.     putchar (c);
  157.     }
  158.     printf ("\n");
  159.   }
  160. }
  161.  
  162. void
  163. dump_header (struct pi_file *pf, struct DBInfo *ip)
  164. {
  165.   printf ("name: \"%s\"\n", ip->name);
  166.   printf ("flags: 0x%x", ip->flags);
  167.   if (ip->flags & dlpDBFlagResource) printf (" RESOURCE");
  168.   if (ip->flags & dlpDBFlagReadOnly) printf (" READ_ONLY");
  169.   if (ip->flags & dlpDBFlagAppInfoDirty) printf (" APP-INFO-DIRTY");
  170.   if (ip->flags & dlpDBFlagBackup) printf (" BACKUP");
  171.   if (ip->flags & dlpDBFlagOpen) printf (" OPEN");
  172.   printf ("\n");
  173.   printf ("version: %d\n", ip->version);
  174.   printf ("creation_time: %s\n", iso_time_str (ip->crdate));
  175.   printf ("modified_time: %s\n", iso_time_str (ip->moddate));
  176.   printf ("backup_time: %s\n", iso_time_str (ip->backupdate));
  177.   printf ("modification_number: %ld\n", ip->modnum);
  178.   printf ("type: '%s', ", printlong(ip->type));
  179.   printf ("creator: '%s'\n", printlong(ip->creator));
  180.   printf ("\n");
  181. }
  182.  
  183. void
  184. dump_app_info (struct pi_file *pf, struct DBInfo *ip)
  185. {
  186.   void *app_info;
  187.   int app_info_size;
  188.  
  189.   if (pi_file_get_app_info (pf, &app_info, &app_info_size) < 0) {
  190.     printf ("can't get app_info\n\n");
  191.     return;
  192.   }
  193.  
  194.   printf ("app_info_size %d\n", app_info_size);
  195.   dump (app_info, app_info_size);
  196.   printf ("\n");
  197. }
  198.  
  199. void
  200. dump_sort_info (struct pi_file *pf, struct DBInfo *ip)
  201. {
  202.   void *sort_info;
  203.   int sort_info_size;
  204.  
  205.   if (pi_file_get_sort_info (pf, &sort_info, &sort_info_size) < 0) {
  206.     printf ("can't get sort_info\n\n");
  207.     return;
  208.   }
  209.  
  210.   printf ("sort_info_size %d\n", sort_info_size);
  211.   dump (sort_info, sort_info_size);
  212.   printf ("\n");
  213. }
  214.  
  215. void
  216. list_records (struct pi_file *pf, struct DBInfo *ip)
  217. {
  218.   int entnum;
  219.   int size;
  220.   unsigned long type, uid;
  221.   int id;
  222.   void *buf;
  223.   int nentries;
  224.   int attrs, cat;
  225.  
  226.  
  227.   pi_file_get_entries (pf, &nentries);
  228.  
  229.   if (ip->flags & dlpDBFlagResource) {
  230.     printf ("entries\n");
  231.     printf ("index\tsize\ttype\tid\n");
  232.     for (entnum = 0; entnum < nentries; entnum++) {
  233.       if (pi_file_read_resource (pf, entnum, &buf, &size, &type, &id) < 0) {
  234.         printf ("error reading %d\n\n", entnum);
  235.         return;
  236.       }
  237.       printf ("%d\t%d\t%s\t%d\n", entnum, size, printlong(type), id);
  238.       if (vflag) {
  239.         dump(buf,size);
  240.         printf ("\n");
  241.       }
  242.     }
  243.   } else {
  244.     printf ("entries\n");
  245.     printf ("index\tsize\tattrs\tcat\tuid\n");
  246.     for (entnum = 0; entnum < nentries; entnum++) {
  247.       if (pi_file_read_record (pf, entnum, &buf, &size,
  248.                    &attrs, &cat, &uid) < 0) {
  249.         printf ("error reading %d\n\n", entnum);
  250.         return;
  251.       }
  252.       printf ("%d\t%d\t0x%x\t%d\t0x%lx\n", entnum, size, attrs, cat, uid);
  253.       if (vflag) {
  254.         dump(buf,size);
  255.         printf ("\n");
  256.       }
  257.     }
  258.   }
  259.  
  260.   printf ("\n");
  261. }
  262.  
  263. void
  264. dump_record (struct pi_file *pf, struct DBInfo *ip, int record)
  265. {
  266.   int size;
  267.   unsigned long type, uid;
  268.   int id;
  269.   void *buf;
  270.   int attrs, cat;
  271.  
  272.   if (ip->flags & dlpDBFlagResource) {
  273.     printf ("entries\n");
  274.     printf ("index\tsize\ttype\tid\n");
  275.     if (pi_file_read_resource (pf, record, &buf, &size, &type, &id) < 0) {
  276.       printf ("error reading resource #%d\n\n", record);
  277.       return;
  278.     }
  279.     printf ("%d\t%d\t%s\t%d\n", record, size, printlong(type), id);
  280.     dump(buf,size);
  281.   } else {
  282.     printf ("entries\n");
  283.     printf ("index\tsize\tattrs\tcat\tuid\n");
  284.     if (pi_file_read_record (pf, record, &buf, &size,
  285.                  &attrs, &cat, &uid) < 0) {
  286.       printf ("error reading record #%d\n\n", record);
  287.       return;
  288.     }
  289.     printf ("%d\t%d\t0x%x\t%d\t0x%lx\n", record, size, attrs, cat, uid);
  290.     dump(buf,size);
  291.   }
  292.  
  293.   printf ("\n");
  294. }
  295.  
  296.